package com.example.broadcastlistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import java.lang.ref.WeakReference;
import java.util.Set;


/**
 * @author hdz
 * @date 2019/06/22
 */
public class BroadcastListener {

    private WeakReference<Context> mWeakContext;
    private MessageReceiver mMessageReceiver;

    private static final String[] ACTIONS = {
            "cn.manstep.phonemirrorBox.AUTO_BOX_CONTROL_CMD_EVT",
            "android.intent.action.CLOSE_SYSTEM_DIALOGS",
            "android.media.VOLUME_CHANGED_ACTION",

            "ACTION_IVCAR_OPERATION",
            "com.microntek.canbusdisplay",
            "com.microntek.irkeyDown",
            "com.microntek.irkeyUp",
            "com.microntek.eqchange",
            "com.microntek.bootcheck"

            , "android.intent.action.SCREEN_ON"
            , "android.intent.action.SCREEN_OFF"
            , "com.nwd.action.ACTION_KEY_VALUE"
            , "android.media.STREAM_DEVICES_CHANGED_ACTION"

            , "com.nwd.action.ACTION_VOLUME_STATE_CHANGE"
            , "com.nwd.ACTION_MUTE_STATE_CHANGE"
            , "com.nwd.ACTION_SET_ARM_VOLUME_STATE"

            , "android.media.VOLUME_CHANGED_ACTION"
            , "android.media.STREAM_MUTE_CHANGED_ACTION"
            , "android.media.INTERNAL_RINGER_MODE_CHANGED_ACTION"
//            , android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION
            , android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION

//            , android.net.wifi.WifiManager.RSSI_CHANGED_ACTION
//            , "android.net.wifi.SCAN_RESULTS"
//            , "android.net.conn.CONNECTIVITY_CHANGE"
//            , "android.net.conn.INET_CONDITION_ACTION"
//            , "com.android.server.action.NETWORK_STATS_UPDATED"
//            , "android.net.wifi.observed_accesspionts_changed"
            , "android.bluetooth.adapter.action.STATE_CHANGED"
    };

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle bundle = intent.getExtras();
            StringBuilder info = new StringBuilder();
            if (bundle != null) {
                Set<String> getKey = bundle.keySet();
                for (String key : getKey) {
                    Object object = bundle.get(key);
                    if (object instanceof Integer) {
                        info.append("\n").append(key).append("==int==").append(object);
                    } else if (object instanceof String) {
                        info.append("\n").append(key).append("==String==").append(object);
                    } else if (object instanceof Boolean) {
                        info.append("\n").append(key).append("==boolean==").append(object);
                    } else {
                        if (object != null) {
                            info.append("\n").append(key).append("==").append(object.getClass()).append("==").append(object);
                        } else {
                            info.append("\n").append(key).append("==").append("null").append("==").append("null");
                        }
                    }
                }
                if (isOrderedBroadcast()) {
                    abortBroadcast();
                }
            } else {
                info = new StringBuilder("no extras");
            }
            if (mMessageReceiver != null) {
                mMessageReceiver.onReceive(action, info.toString());
            }
        }
    };

    public BroadcastListener(Context context, MessageReceiver messageReceiver) {
        IntentFilter intentFilter = new IntentFilter();
        for (String action : ACTIONS) {
            intentFilter.addAction(action);
        }
        mMessageReceiver = messageReceiver;
        intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        context.registerReceiver(broadcastReceiver, intentFilter);
        mWeakContext = new WeakReference<>(context);
    }

    public void destroy() {
        Context context = mWeakContext.get();
        if (context != null) {
            context.unregisterReceiver(broadcastReceiver);
        }
    }

    public interface MessageReceiver {
        void onReceive(String action, String detail);
    }
}
